﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Omnia.DataModels;
using Omnia.Library;
using System.Linq;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Omnia.Library.Extensions;

namespace Omnia.ViewModels
{
    public class CaseDetailsViewModel : CaseViewModel
    {
        #region Properties
        private string guid;
        public string Guid
        {
            get { return guid; }
            set
            {
                guid = value;
                RaisePropertyChanged(() => Guid);
                CurrentEditCaseItem = ShowCasesCollection.FirstOrDefault(c => c.Guid == Guid);
                RaisePropertyChanged(() => CurrentEditCaseItem);
                RaisePropertyChanged(() => LinkedTransactionCollection);
            }
        }

        private CaseInfo currentEditCaseItem;
        public CaseInfo CurrentEditCaseItem
        {
            get { return currentEditCaseItem; }
            set
            {
                currentEditCaseItem = value;
                RaisePropertyChanged(() => CurrentEditCaseItem);
            }
        }

        public ObservableCollection<TransactionInfo> LinkedTransactionCollection
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(Guid))
                {
                    var items = Database.CollectionOfTransactions.Where(t => t.CaseGuid == Guid);
                    foreach (TransactionInfo t in items)
                    {
                        t.Category = Database.CollectionOfCategories.FirstOrDefault(c => c.Guid == t.CategoryGuid);
                        if (!string.IsNullOrWhiteSpace(t.CaseGuid))
                        {
                            t.Case = Database.CollectionOfCases.FirstOrDefault(c => c.Guid == t.CaseGuid);
                        }
                    }

                    return new ObservableCollection<TransactionInfo>(items);
                }
                return null;
            }
        }
        #endregion

        #region Method
        private void Remove(CaseInfo entity)
        {
            var trans = Database.CollectionOfTransactions.Where(t => t.CaseGuid == entity.Guid);
            if (trans != null)
            {
                foreach (TransactionInfo t in trans)
                {
                    t.CaseGuid = null;
                }
            }

            Database.CollectionOfCases.DeleteOnSubmit(entity);
            Save();
        }

        private void Update(CaseInfo entity)
        {
            var item = Database.CollectionOfCases.FirstOrDefault(c => c.Guid == entity.Guid);
            item = entity;
            Save();
        }
        #endregion

        #region Commands
        public ICommand UpdateCaseCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        Update(CurrentEditCaseItem);
                        NavigateBack();
                    }
                };
            }
        }

        public ICommand DeleteCaseCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        if (MessageBox.Show(Application.Current.FindResource<string>(ResourceKeys.IDS_NOTE_CASE_CONFIRM_DELETE), Application.Current.FindResource<string>(ResourceKeys.IDS_NOTE_CASE_CONFIRM_TITLE), MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            Remove(CurrentEditCaseItem);
                            NavigateBack();
                        }
                    }
                };
            }
        }
        #endregion
    }
}
